﻿using duia.Canteenpay.Domain.DomainObject;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;

namespace duia.Canteenpay.Repository.EntityConfiguration
{
    /// <summary>
    /// 菜品类型用户-ef映射管理
    /// </summary>
    public class DishesCategoryEntityTypeConfiguration : BaseEntityTypeConfiguration<DishesCategory>
    {
        public override void Configure(EntityTypeBuilder<DishesCategory> builder)
        {
            builder.ToTable("canpay_dishes_category");
            builder.HasKey(x => x.KeyId);
            builder.Property(x => x.Name).IsRequired().HasMaxLength(50);
            //一对多配置
            builder.HasMany(x => x.Dishes)
                .WithOne()
                .HasForeignKey(x => x.DishesCategoryKeyId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.HasData(AddHasData());
            base.Configure(builder);
        }

        private List<DishesCategory> AddHasData()
        {
            var hasData = new List<DishesCategory>();
            var dishesCategory = new DishesCategory("荤菜");
            dishesCategory.SetKeyId(Guid.Parse("09f0d3c7-40fe-11eb-8fac-5254000e3ad6"));

            hasData.Add(dishesCategory);
            return hasData;
        }
    }
}
